<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

<#if enableCache>
    <!-- 开启二级缓存 -->
    <cache type="${cacheClassName}"/>

</#if>
<#if baseResultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.name}" property="${field.propertyName}" jdbcType="${field.metaInfo.jdbcType}" />
</#if>
</#list>
<#list table.commonFields as field><#--生成公共字段 -->
        <result column="${field.name}" property="${field.propertyName}" jdbcType="${field.metaInfo.jdbcType}" />
</#list>
<#list table.fields as field>
<#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" property="${field.propertyName}" jdbcType="${field.metaInfo.jdbcType}" />
</#if>
</#list>
    </resultMap>
</#if>

<#if baseColumnList>
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
<#list table.commonFields as field>
        ${field.columnName},
</#list>
        ${table.fieldNames}
    </sql>
</#if>

    <!--  根据id查询  -->
    <select id="select${entity}ById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        SELECT <include refid="Base_Column_List"/>
        FROM ${table.name} WHERE  id = ${r'#{id}'}
    </select>

    <!--  批量查询  -->
    <select id="select${entity}List" resultMap="BaseResultMap" parameterType="${package.Entity}.${entity}">
        SELECT <include refid="Base_Column_List"/> FROM ${table.name}
        <trim prefix="where" prefixOverrides="AND || OR">
    <#list table.fields as field>
        <#if field.propertyType == 'Long' || field.propertyType == 'Integer' || field.propertyType == 'Date' >
            <if test="${field.propertyName} !=null">AND ${field.name} = ${"#{"+field.propertyName+"}"}</if>
        <#else>
            <if test="${field.propertyName} !=null and ${field.propertyName}!=''">AND ${field.name} = ${"#{"+field.propertyName+"}"}</if>
        </#if>
    </#list>
        </trim>
        <trim prefix="ORDER BY " prefixOverrides="ORDER BY ">
            <if test="sort !=null and sort != ''">
                ${r'#{sort}'}
            </if>
        </trim>
    </select>

    <!--  分页查询  -->
    <select id="select${entity}ListByPage" resultMap="BaseResultMap" parameterType="${package.Entity}.${entity}">
        SELECT <include refid="Base_Column_List"/> FROM ${table.name}
        <trim prefix="where" prefixOverrides="AND || OR">
            <#list table.fields as field>
                <#if field.propertyType == 'Long' || field.propertyType == 'Integer' || field.propertyType == 'Date' >
                    <if test="${field.propertyName} !=null">AND ${field.name} = ${"#{"+field.propertyName+"}"}</if>
                <#else>
                    <if test="${field.propertyName} !=null and ${field.propertyName}!=''">AND ${field.name} = ${"#{"+field.propertyName+"}"}</if>
                </#if>
            </#list>
        </trim>
        <trim prefix="ORDER BY " prefixOverrides="ORDER BY ">
            <if test="sort !=null and sort != ''">
                ${r'#{sort}'}
            </if>
        </trim>
    </select>

    <!--  新增  -->
    <insert id="insert${entity}" parameterType="${package.Entity}.${entity}">
        INSERT INTO ${table.name}
        <trim prefix="(" suffix=")" suffixOverrides=",">
    <#list table.fields as field>
        <#if field.keyFlag ><#--  生成主键排在第一位  -->
        <#else>
            <#if field.propertyType == 'Long' || field.propertyType == 'Integer' || field.propertyType == 'Date'>
            <if test="${field.propertyName} !=null">${field.name},</if>
            <#else>
            <if test="${field.propertyName} !=null and ${field.propertyName}!=''">${field.name},</if>
            </#if>
        </#if>
    </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
    <#list table.fields as field>
        <#if field.keyFlag ><#--  生成主键排在第一位  -->
        <#else>
            <#if field.propertyType == 'Long' || field.propertyType == 'Integer' || field.propertyType == 'Date'>
            <if test="${field.propertyName} !=null">${"#{"+field.propertyName+"}"},</if>
            <#else>
            <if test="${field.propertyName} !=null and ${field.propertyName}!=''">${"#{"+field.propertyName+"}"},</if>
            </#if>
        </#if>
    </#list>
        </trim>
    </insert>

    <!--  根据id修改  -->
    <update id="update${entity}ById" parameterType="${package.Entity}.${entity}">
        UPDATE ${table.name}
        <set>
    <#list table.fields as field>
        <#if field.propertyType == 'Long' || field.propertyType == 'Integer' || field.propertyType == 'Date'>
            <#if field.propertyName != 'id'>
            <if test="${field.propertyName} !=null">${field.name} = ${"#{"+field.propertyName+"}"},</if>
            </#if>
        <#else>
            <if test="${field.propertyName} !=null and ${field.propertyName}!=''">${field.name} = ${"#{"+field.propertyName+"}"},</if>
        </#if>
    </#list>
        </set>
        WHERE id = ${r'#{id}'}
    </update>

    <!--  根据id删除  -->
    <delete id="delete${entity}ById" parameterType="java.lang.Long">
        DELETE FROM ${table.name}
        WHERE id = ${r'#{id}'}
    </delete>

</mapper>
